﻿using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;

namespace SecurityModule
{
    internal class AssemblyDetails
    {
        public FileInfo FileInfo { get; private set; }
        public string SHA1Hash { get; private set; }
        public string PublicKeyToken { get; private set; }
        public bool? PassedValidation { get; private set; }

        public AssemblyDetails(string filePath)
        {
            FileInfo = new FileInfo(filePath);
            byte[] hash;
            using (var sha1 = SHA1.Create())
            {
                using (var stream = File.OpenRead(filePath))
                {
                    hash = sha1.ComputeHash(stream);
                }
            }
            SHA1Hash = hash.ByteArrayToString();
            PublicKeyToken = Assembly.LoadFile(filePath).GetName().GetPublicKeyToken().ByteArrayToString();
            if (ExpectedAssemblyDetails.Instance.Data.ContainsKey(FileInfo.Name))
            {
                var expected = ExpectedAssemblyDetails.Instance.Data[FileInfo.Name];
                PassedValidation = true;
                if (expected.SHA1Hash!=null)
                {
                    if (!expected.SHA1Hash.Equals(SHA1Hash))
                        PassedValidation = false;
                }
                if (expected.PublicKeyToken != null)
                {
                    if (!expected.PublicKeyToken.Equals(PublicKeyToken))
                        PassedValidation = false;
                }
            }
            else
            {
                PassedValidation = null;
            }
        }
    }
}